package com.wang.auto.addSet;


import com.wang.conf.AppConf;
import com.wang.conf.AppData;
import com.wang.packpojo.AddSetMsg;
import com.wang.packpojo.ConMsg;
import com.wang.pojo.*;
import com.wang.service.*;
import com.wang.socket.OutBoundHandler;
import com.wang.socket.SocketMap;
import com.wang.util.*;
import com.wang.webSocket.WebSocketUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.SneakyThrows;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class AddSetAutoSend {


    static String flagConf = "0258";
    private static  Long getAddSetMsgTime = 1000*60*20L;  //间隔20分
    //String 设备devGId
    public static  Map<String, AddSetMsg> addSetDevMap  = new ConcurrentHashMap<>();

    public static  void  init(){

        //间隔获取设备附加配置数据
        Timer timer1 = new Timer();
        timer1.schedule(new TimerTask() {

            public void run() {
                getAddSetMsg();
            }
        }, new Date(), getAddSetMsgTime);

        //启动发送检测指令
        checkStart();

    }

    //启动设备进行监测
    public static void checkStart(){

        new Thread(()->{
            while (true){
                if(addSetDevMap.size()>0){
                    addSetDevMap.forEach((key,value)->{



                        //获取设备状态 暂时从数据库中获取
                        String devGId = key;
                        AddSetMsg addSetMsg = value;

                        //获取当前时间
                        long currentTime = new Date().getTime();
                        if(addSetMsg.getSendStartTime() ==0 || currentTime-addSetMsg.getSendStartTime()>=(addSetMsg.getAutoCollectRate())*1000*60){

                            Map<String, ConMsg> mRegPassDeviceMap = SocketMap.mRegPassDeviceMap;
                            mRegPassDeviceMap.forEach((key1,value1)->{

                                ConMsg conMsg = value1;
                                Device device = conMsg.getDevice();
                                if(device.getDevGId().equals(devGId)){
                                    long  regTime = conMsg.getRegTime(); //注册时间
                                    //判断设备注册时间
                                    if(currentTime-regTime>= AppConf.delayTime ) {


                                        //设备在线

                                        //获取启动设备测量的指令
                                        List<AddSetCheckData> addSetCheckDataList = addSetMsg.getAddSetCheckDataList();
                                        //如果现在是自动模式，并且满足雨水液位大于界限值
                                        AddAutoSetDataService addAutoSetDataService = SpringUtil.getBean("addAutoSetDataServiceImpl",AddAutoSetDataServiceImpl.class);
                                        AddAutoSetData addAutoSetData = addAutoSetDataService.selectByDevGId(devGId);
                                        //获取最新的值
                                        CheckDataService checkDataService = SpringUtil.getBean("checkDataServiceImpl",CheckDataServiceImpl.class);
                                        CheckData checkData = checkDataService.selectLastParamCheck(device.getCheckDataTable(),device.getDevCheckTable(),addAutoSetData.getParamName());
                                        Double lastValue = checkData != null && EmptyUtil.isNotNull(checkData.getCheckDataValue()) ? Double.valueOf(checkData.getCheckDataValue()) : 0;
                                        //自动化模式并且 小于界限值
                                        if(addSetMsg.getNowStatus().equals(1) && lastValue.intValue() <addAutoSetData.getLimitValue() ){
                                            System.out.println("不满足发送测量指令条件："+lastValue.intValue()+":"+addAutoSetData.getLimitValue());

                                        }else {
                                            //发送指令
                                            addSetCheckDataList.forEach(addSetCheckData -> {

                                                String command = addSetCheckData.getData();
                                                byte[] commandByte = ModBusUtil.HexString2Bytes(command);
                                                OutBoundHandler.outData(conMsg.getCtx(), commandByte);

                                                PrintUtil.print(devGId + addSetCheckData.getParamName() + "启动测量" + "指令:" + command);

                                                WebSocketUtil.sendTestData(device.getDevGId(), addSetCheckData.getParamName() + "启动测量" + "指令:" + command);

                                                try {
                                                    Thread.sleep(10000L);
                                                } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                                }


                                                //记录设备日志
                                                DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                                                //记录日志
                                                DevLog devLog = new DevLog();
                                                devLog.setDevLogTypeCode("0059");
                                                devLog.setDevEventTypeCode("0141");
                                                devLog.setDevLogContent(addSetCheckData.getParamName() + "启动测量");
                                                devLog.setCreateTime(DateUtil.getStrDate());
                                                devLog.setDevId(device.getDevId().toString());
                                                devLog.setIsRead(1);
                                                devLogService.addDevLog(devLog);


                                            });
                                            /*//发送指令2
                                            addSetCheckDataList.forEach(addSetCheckData -> {

                                                String command = addSetCheckData.getData();
                                                byte[] commandByte = ModBusUtil.HexString2Bytes(command);
                                                OutBoundHandler.outData(conMsg.getCtx(), commandByte);

                                                PrintUtil.print(devGId + addSetCheckData.getParamName() + "启动测量" + "指令:" + command);

                                                WebSocketUtil.sendTestData(device.getDevGId(), addSetCheckData.getParamName() + "启动测量" + "指令:" + command);

                                                try {
                                                    Thread.sleep(10000L);
                                                } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                                }


                                                //记录设备日志
                                                DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                                                //记录日志
                                                DevLog devLog = new DevLog();
                                                devLog.setDevLogTypeCode("0059");
                                                devLog.setDevEventTypeCode("0141");
                                                devLog.setDevLogContent(addSetCheckData.getParamName() + "启动测量2");
                                                devLog.setCreateTime(DateUtil.getStrDate());
                                                devLog.setDevId(device.getDevId().toString());
                                                devLog.setIsRead(1);
                                                devLogService.addDevLog(devLog);


                                            });*/

                                            //更新发送时间
                                            addSetMsg.setSendStartTime(new Date().getTime());
                                        }
                                    }

                                }
                            });

                        }


                    });
                }

                try {
                    //每隔10秒检查一次
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }



    //获取设备检测相关数据
    public static void  getAddSetMsg(){


        AddSetService addSetService = SpringUtil.getBean("addSetServiceImpl",AddSetServiceImpl.class);
        List<AddSet> addSetList = addSetService.list();

        if(addSetList.size()>0){
            for (AddSet addSet : addSetList){
                DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);

                Device device = deviceService.selectDeviceByDevGId(addSet.getDevGId());
                AddSetMsg addSetMsg = new AddSetMsg();


                addSetMsg.setDevId(device.getDevId());
                addSetMsg.setDevGId(device.getDevGId());
                addSetMsg.setNowStatus(addSet.getNowStatus());
                addSetMsg.setCheckStatus(addSet.getCheckStatus());

                //查询自动化配置
                AddAutoSetDataService addAutoSetDataService = SpringUtil.getBean("addAutoSetDataServiceImpl",AddAutoSetDataServiceImpl.class);
                AddAutoSetData addAutoSetData = addAutoSetDataService.selectBySetId(addSet.getSetId());



                addSetMsg.setAddAutoSetData(addAutoSetData);

                //查询手动化配置
                AddManualSetDataService addManualSetDataService = SpringUtil.getBean("addManualSetDataServiceImpl",AddManualSetDataServiceImpl.class);
                AddManualSetData addManualSetData = addManualSetDataService.selectBySetId(addSet.getSetId());

                addSetMsg.setAddManualSetData(addManualSetData);

                if(addSet.getNowStatus().equals(1)){
                    addSetMsg.setAutoCollectRate(addAutoSetData.getAutoCollectRate());
                }else {
                    addSetMsg.setAutoCollectRate(addManualSetData.getAutoCollectRate());
                }

                //查询启动设备指令参数
                AddSetCheckDataService checkDataService = SpringUtil.getBean("addSetCheckDataServiceImpl",AddSetCheckDataServiceImpl.class);
                List<AddSetCheckData> addSetCheckDataList = checkDataService.selectByDevGId(device.getDevGId());


                addSetMsg.setAddSetCheckDataList(addSetCheckDataList);
                addSetDevMap.put(device.getDevGId(),addSetMsg);

            }
        }

    }
    //更新设备检测相关数据
    public static void  updateAddSetMsg(String devGId){

        DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);
        Device device = deviceService.selectDeviceByDevGId(devGId);


        AddSetMsg addSetMsg = new AddSetMsg();
        //查询附加设置
        AddSetService addSetService = SpringUtil.getBean("addSetServiceImpl", AddSetServiceImpl.class);
        AddSet addSet =  addSetService.selectByDevId(device.getDevGId());

        addSetMsg.setDevId(device.getDevId());
        addSetMsg.setDevGId(device.getDevGId());
        addSetMsg.setNowStatus(addSet.getNowStatus());
        addSetMsg.setCheckStatus(addSet.getCheckStatus());

        //查询自动化配置
        AddAutoSetDataService addAutoSetDataService = SpringUtil.getBean("addAutoSetDataServiceImpl",AddAutoSetDataServiceImpl.class);
        AddAutoSetData addAutoSetData = addAutoSetDataService.selectBySetId(addSet.getSetId());




        addSetMsg.setAddAutoSetData(addAutoSetData);

        //查询手动化配置
        AddManualSetDataService addManualSetDataService = SpringUtil.getBean("addManualSetDataServiceImpl",AddManualSetDataServiceImpl.class);
        AddManualSetData addManualSetData = addManualSetDataService.selectBySetId(addSet.getSetId());

        addSetMsg.setAddManualSetData(addManualSetData);

        if(addSet.getNowStatus().equals(1)){
            addSetMsg.setAutoCollectRate(addAutoSetData.getAutoCollectRate());
        }else {
            addSetMsg.setAutoCollectRate(addManualSetData.getAutoCollectRate());
        }

        //查询启动设备指令参数
        AddSetCheckDataService checkDataService = SpringUtil.getBean("addSetCheckDataServiceImpl",AddSetCheckDataServiceImpl.class);
        List<AddSetCheckData> addSetCheckDataList = checkDataService.selectByDevGId(device.getDevGId());

        addSetMsg.setAddSetCheckDataList(addSetCheckDataList);
       addSetDevMap.put(devGId,addSetMsg);



    }


}
